import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '银行管理项目')))
from models.models import db, User, UserAuth
from app import app

def fix_user_auth_user_id():
    with app.app_context():
        count = 0
        customers = UserAuth.query.filter_by(role='customer').all()
        for ua in customers:
            user = User.query.filter_by(phone=ua.username).first()
            if user and ua.user_id != user.id:
                ua.user_id = user.id
                count += 1
        db.session.commit()
        print(f'已修复 {count} 个 user_auth 账号的 user_id 字段！')

if __name__ == '__main__':
    fix_user_auth_user_id() 